using System;
using System.IO;

namespace CWare.CMetrics.Core
{
public static class Metrics
{
    private static Int64 GeneralPerLineMetric(string filepath, Func<CSharpCodeLine, bool> doesLineCount )
    {
        if (filepath == null)
        {
            throw new ArgumentNullException();
        }
        var fileReader = new StreamReader(new FileStream(filepath, FileMode.Open));
        string lineText = null;
        Int64 result = 0;
        do
        {
            lineText = fileReader.ReadLine();
            var line = new CSharpCodeLine(lineText);
            if ( line.Content!=null && doesLineCount(line) )
            {
                ++result;
            }
        }
        while ( lineText != null );
        fileReader.Close();
        fileReader.Dispose();
        return result;
    }

    public static Int64 SLOC(string filepath)
    {
        return GeneralPerLineMetric(filepath, line => line.isNotEmpty);
    }

    public static Int64 ELOC(string filepath)
    {
        return GeneralPerLineMetric(filepath, line => line.isEffectiveCodeLine);
    }

}
}
